define(['moment'], function (moment) {
    return ['$scope', '$filter', '$stateParams', 'config', 'MMWService', '$mdDialog','$timeout','$css', '$compile',
        function ($scope, $filter, $stateParams, config, $MMWService,$mdDialog,$timeout,$css, $compile) 
        {
            $scope.init = function()
            {
                $scope.main_page_url = 'module/moduleQT/program/QT05/QT05_Query_Page.html';
                $scope.query_event = QueryPageSettingEvent();
                $scope.main_event = MainPageEvent();
                $scope.sheetGrid_event = SheetGridEvent();
                $scope.query_setting = [];
                $scope.targetSheet = '';
                $scope.sheetData = [];
                $scope.getPages = getPages;
            };
            var getQuerySetting = function(){
                return  $scope.query_setting;
            }
            var setQuerySetting = function(query_data) {
                $scope.query_setting = query_data;
            }
            var changeMainPage = function(url) 
            {
                $scope.main_page_url = url;
            }
            var qualityTraceInfoGet = function (content,callback) 
            {
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.quality_trace_info_get',
                    content : content,
                    success : function(data)
                    {
                        callback && callback(data);
                    }
                });
            }
            var setTargetSheet = function(sheet) 
            {
                $scope.targetSheet = sheet;
            }
            var getTargetSheet = function() 
            {
                return $scope.targetSheet || '';
            }
            var setSheetData = function(data) {
                $scope.sheetData = data;
            }
            var getSheetData = function(data) {
                return $scope.sheetData;
            }
            var exportExcel = function(content) {
                if(window.nodeRequire) {
                    var dialog = window.nodeRequire('electron').remote.dialog;
                    dialog.showOpenDialog({
                        properties: ['openDirectory']
                    },function(fileList){
                        if(fileList && fileList.length>0){
                            qualityTraceInfoGet(content, function(data) {
                                var file_data = Buffer.from(data.file_data, 'base64');
                                var file_name = fileList[0]+'\\'+ data.name + moment().format('YYYYMMDDHHmmss')+'.xls';
                                window.nodeRequire('fs').writeFile(file_name, file_data, function(error){
                                    if(error){
                                        $rootScope.showAlert($filter('translate')('common.msg.cantDownloadFile')+error);
                                    } else {
                                        $mdlToast.hide();
                                        $rootScope.showConfirm($filter('translate')('common.msg.file_download_complete_and_open'), function(dialog){
                                            dialog.hide();
                                            window.nodeRequire('electron').shell.openItem(file_name);
                                        });
                                    }
                                }); 
                            })
                        }
                    });
                } else {
                    $rootScope.showToast($filter('translate')('common.msg.file_download_complete'));
                }
            }
            
            var getPages = function(list, PageConfig){
                if (list) {
                    if (PageConfig) {
                        PageConfig.total_page = Math.ceil(list.length / PageConfig.page_count) || 1; //最大頁數 (陣列長度/每頁比數)
                        if (PageConfig.page >= PageConfig.total_page) {
                            PageConfig.page = 0;
                        }
                        var index = PageConfig.page * PageConfig.page_count;
                        return list.slice(index, index + PageConfig.page_count); //回傳該頁數的內容
                    } else {
                        return list;
                    }
                } else {
                    return list;
                }
            }

            var QueryPageSettingEvent = function()
            {
                var  SettingEvent = 
                {
                    query_label_list:
                    [
                        {l_type:"normal",code:"A.ITEM_CODE",label:$filter('translate')('QT05.query.ITEM_CODE')},
                        {l_type:"normal",code:"A.ITEM_NAME",label:$filter('translate')('QT05.query.ITEM_NAME')},
                        {l_type:"normal",code:"A.ITEM_SPEC",label:$filter('translate')('QT05.query.ITEM_SPEC')},
                        {l_type:"normal",code:"A.LOTNO",label:$filter('translate')('QT05.query.LOTNO')},
                        {l_type:"normal",code:"A.LOT_CODE",label:$filter('translate')('QT05.query.LOT_CODE')},
                        {l_type:"normal",code:"A.MONO",label:$filter('translate')('QT05.query.MONO')},
                        {l_type:"normal",code:"A.RONO",label:$filter('translate')('QT05.query.RONO')},
                        {l_type:"normal",code:"A.CUSTOMERNO",label:$filter('translate')('QT05.query.CUSTOMERNO')},
                        {l_type:"normal",code:"A.CUSTOMERNAME",label:$filter('translate')('QT05.query.CUSTOMERNAME')},
                        {l_type:"date",code:"A.ENTRY_DATE",label:$filter('translate')('QT05.query.ENTRY_DATE')},
                        {l_type:"single",code:"A.PCSNo",label:$filter('translate')('QT05.query.PCSNo')}
                    ],
                    query_judgment_list:[
                        {code:"0",label:$filter('translate')('QT05.query_judgment.equal')},
                        {code:"2",label:$filter('translate')('QT05.query_judgment.contain')}
                    ],
                    select_label_item:{},
                    select_type_item:{},
                    select_txt_vale: '',
                    select_date_vale:
                    {
                        start_time:moment(new Date()).toDate(),
                        end_time:moment(new Date()).add(1,'days').toDate()
                    },
                    condition:[],
                    query_setting_detail:[],
                    PageConfig: {
                        page: 0,
                        page_count: 25
                    },
                    init:function() 
                    {   
                        // 默认筛选条件默认生产批号(3)
                        SettingEvent.select_label_item = SettingEvent.query_label_list[3];
                        SettingEvent.select_type_item = SettingEvent.query_judgment_list[0];
                        SettingEvent.condition = [];
                        SettingEvent.query_setting_detail = [];
                        SettingEvent.select_date_vale =
                        {
                            start_time:moment(new Date()).toDate(),
                            end_time:moment(new Date()).add(1,'days').toDate()
                        }
                    },
                    loadCodeList:function (item) {
                        $scope.showSelect({
                            title : $filter('translate')('common.report.condition_fields'),
                            label : 'label',
                            code : 'code',
                            selectCode : item.code,
                            list : SettingEvent.query_label_list,
                            confirm : function(selectItem, dialog){
                                SettingEvent.select_label_item = selectItem;
                                if(item.l_type == 'date')
                                    SettingEvent.select_date_vale = 
                                    {
                                        start_time:moment(new Date()).toDate(),
                                        end_time:moment(new Date()).add(1,'days').toDate()
                                    }
                                else
                                    SettingEvent.select_txt_vale = '';
                                dialog.hide();
                            }
                        });
                    },
                    loadTypeList:function(item) 
                    {
                        $scope.showSelect({
                            title : $filter('translate')('common.condition_title'),
                            label : 'label',
                            code : 'code',
                            selectCode : item.code,
                            list : SettingEvent.query_judgment_list,
                            confirm : function(selectItem, dialog){
                                SettingEvent.select_type_item = selectItem;
                                dialog.hide();
                            }
                        });
                    },
                    removeConditionItem:function(item,index) 
                    {
                        SettingEvent.condition.splice(index,1);
                        if(SettingEvent.condition.length > 0)
                            SettingEvent.search();
                        else
                            SettingEvent.query_setting_detail = [];
                    },
                    search:function() {
                        var content = {};
                        content.query_condition = [];
                        for(var i in SettingEvent.condition)
                        {
                            var cod_item = SettingEvent.condition[i];
                            var query_obj = {};
                            if(cod_item.l_type != "date")
                            {
                                query_obj.column_name = cod_item.column_name;
                                query_obj.require = false;
                                query_obj.query_mode = cod_item.query_mode;
                                query_obj.merge_condition_model = cod_item.merge_condition_model;
                                query_obj.value = cod_item.value;
                                content.query_condition.push(query_obj);
                            }
                            else
                            {
                                var time_list = cod_item.value.split("~");
                                var start_time = time_list[0] +" 00:00:00";
                                var end_time = time_list[1] +" 23:59:59";
                                query_obj.column_name = cod_item.column_name;
                                query_obj.require = false;
                                query_obj.query_mode = 4;
                                query_obj.merge_condition_model = cod_item.merge_condition_model;
                                query_obj.value = start_time;
                                content.query_condition.push(query_obj);
                                query_obj = {};
                                query_obj.column_name = cod_item.column_name;
                                query_obj.require = false;
                                query_obj.query_mode = 5;
                                query_obj.merge_condition_model = cod_item.merge_condition_model;
                                query_obj.value = end_time;
                                content.query_condition.push(query_obj);
                            }
                        }
                        qualityTraceInfoGet(content,function(data) 
                        {
                            if(data.filter_condition_details)
                            {
                                SettingEvent.query_setting_detail =  data.filter_condition_details;
                                for(var i in SettingEvent.query_setting_detail)
                                    SettingEvent.query_setting_detail[i].is_select = false;
                            }
                        })
                    },
                    clickQueryItem:function(item) {
                        for(var i in SettingEvent.query_setting_detail)
                            SettingEvent.query_setting_detail[i].is_select = false;
                        if(item.is_select)
                            item.is_select = false;
                        else
                            item.is_select = true;
                    },
                    confirm:function() 
                    {
                        var query_setting = $filter("filter")(SettingEvent.query_setting_detail,{is_select:true},true);
                        if(query_setting.length <= 0)
                            return;
                        setQuerySetting(query_setting[0]);
                        changeMainPage('module/moduleQT/program/QT05/QT05_Main.html');
                    },
                    addCondition:function() 
                    {
                        var has_single = $filter('filter')(SettingEvent.condition,{l_type:"single"},true);
                        if(has_single.length > 0 && SettingEvent.select_label_item.l_type != "single")
                            return;

                        if(SettingEvent.select_label_item.l_type != "date")
                        {
                            if(SettingEvent.select_txt_vale.trim() == '')
                                return;
                            var condition_obj = {};
                            condition_obj.column_name = SettingEvent.select_label_item.code;
                            condition_obj.require = false;
                            condition_obj.merge_condition_model = "AND";
                            condition_obj.query_mode = SettingEvent.select_type_item.code;
                            if(condition_obj.query_mode == "2")
                                condition_obj.value = "%"+SettingEvent.select_txt_vale+"%";
                            else
                                condition_obj.value = SettingEvent.select_txt_vale;
    

                            condition_obj.l_type = SettingEvent.select_label_item.l_type;
                            condition_obj.label_name = SettingEvent.select_label_item.label;
                            condition_obj.query_name = SettingEvent.select_type_item.label;
                            condition_obj.value_txt = SettingEvent.select_txt_vale;
                           
                            var has_repet = $filter('filter')(SettingEvent.condition,condition_obj,true);
                            if(has_repet.length  > 0)
                                return ;
                            if(SettingEvent.select_label_item.l_type == "single")
                            {
                                SettingEvent.condition = $filter('filter')(SettingEvent.condition,{l_type:"single"});
                            }
                            SettingEvent.condition.push(condition_obj);
                        }
                        else
                        {
                            var start_time =  moment(SettingEvent.select_date_vale.start_time).format("YYYY-MM-DD"); 
                            var end_time = moment(SettingEvent.select_date_vale.end_time).format("YYYY-MM-DD"); 
                            
                            var condition_obj = {};
                            condition_obj.column_name = SettingEvent.select_label_item.code;
                            condition_obj.require = false;
                            condition_obj.merge_condition_model = "AND";
                            condition_obj.query_mode = 9;
                            condition_obj.value = start_time  + "~" + end_time;
                            condition_obj.l_type = SettingEvent.select_label_item.l_type;
                            condition_obj.label_name = SettingEvent.select_label_item.label;
                            condition_obj.query_name = '介於';
                            condition_obj.value_txt = start_time  + "~" + end_time;
                            var has_repet = $filter('filter')(SettingEvent.condition,condition_obj,true);
                            if(has_repet.length  > 0)
                                return;

                            SettingEvent.condition.push(condition_obj);
                          
                        }
                        SettingEvent.select_txt_vale = '';
                        SettingEvent.select_date_vale =
                        {
                            start_time:moment(new Date()).toDate(),
                            end_time:moment(new Date()).add(1,'days').toDate()
                        }
                        SettingEvent.search();
                    },
                    getPages:function(list, PageConfig){
                        if (list) {
                            if (PageConfig) {
                                PageConfig.total_page = Math.ceil(list.length / PageConfig.page_count) || 1; //最大頁數 (陣列長度/每頁比數)
                                if (PageConfig.page >= PageConfig.total_page) {
                                    PageConfig.page = 0;
                                }
                                var index = PageConfig.page * PageConfig.page_count;
                                return list.slice(index, index + PageConfig.page_count); //回傳該頁數的內容
                            } else {
                                return list;
                            }
                        } else {
                            return list;
                        }
                    },
                    isConfirmBtnDisabled: function() {
                        return $filter('filter')(SettingEvent.query_setting_detail,{is_select:true},true).length <= 0;
                    },
                    isSearched: function(obj) {
                        return Object.keys(obj).length != 0;
                    },
                    back: function() {
                        changeMainPage('module/moduleQT/program/QT05/QT05_Main.html');
                    }
                };
                return SettingEvent;
            };

            var MainPageEvent = function () 
            {
                var main_event = {
                    bom_query_setting:{},
                    bom_detail:[],
                    bom_select_id:'',
                    all_panel_data:[],
                    panel_list:[
                        {panel_code:'plotno_details',panel_name:$filter('translate')('QT05.sheet.plotno_details.title'),seq:1,visible:true,active:true,url:'module/moduleQT/program/QT05/panel/QT05_plotno_details.html'},
                        {panel_code:'plotno_his_details',panel_name:$filter('translate')('QT05.sheet.plotno_his_details.title'),seq:2,visible:true,active:false,url:'module/moduleQT/program/QT05/panel/QT05_plotno_his_details.html'},
                        {panel_code:'workerlogin_details',panel_name:$filter('translate')('QT05.sheet.workerlogin_details.title'),seq:3,visible:true,active:false,url:'module/moduleQT/program/QT05/panel/QT05_workerlogin_details.html'},
                        {panel_code:'workreport_details',panel_name:$filter('translate')('QT05.sheet.workreport_details.title'),seq:4,visible:true,active:false,url:'module/moduleQT/program/QT05/panel/QT05_workreport_details.html'},
                        {panel_code:'materialopt_details',panel_name:$filter('translate')('QT05.sheet.materialopt_details.title'),seq:5,visible:true,active:false,url:'module/moduleQT/program/QT05/panel/QT05_materialopt_details.html'},
                        {panel_code:'accessoryopt_details',panel_name:$filter('translate')('QT05.sheet.accessoryopt_details.title'),seq:6,visible:true,active:false,url:'module/moduleQT/program/QT05/panel/QT05_accessoryopt_details.html'},
                        {panel_code:'trade_details',panel_name:$filter('translate')('QT05.sheet.trade_details.title'),seq:7,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_trade_details.html'},
                        {panel_code:'process_details',panel_name:$filter('translate')('QT05.sheet.process_details.title'),seq:8,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_process_details.html'},
                        {panel_code:'collect_details',panel_name:$filter('translate')('QT05.sheet.collect_details.title'),seq:9,visible:true,active:false,url:'module/moduleQT/program/QT05/panel/QT05_collect_details.html'},
                        {panel_code:'os_details',panel_name:$filter('translate')('QT05.sheet.os_details.title'),seq:10,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_os_details.html'},
                        {panel_code:'box_details',panel_name:$filter('translate')('QT05.sheet.box_details.title'),seq:11,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_box_details.html'},
                        {panel_code:'machine_details',panel_name:$filter('translate')('QT05.sheet.machine_details.title'),seq:12,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_machine_details.html'},
                        {panel_code:'machineopt_details',panel_name:$filter('translate')('QT05.sheet.machineopt_details.title'),seq:13,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_machineopt_details.html'},
                        {panel_code:'pcsno_details',panel_name:$filter('translate')('QT05.sheet.pcsno_details.title'),seq:14,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_pcsno_details.html'},
                        {panel_code:'sub_product_details',panel_name:$filter('translate')('QT05.sheet.sub_product_details.title'),seq:15,visible:false,active:false,url:'module/moduleQT/program/panel/QT05/QT05_sub_product_details.html'},
                        {panel_code:'inspect_details',panel_name:$filter('translate')('QT05.sheet.inspect_details.title'),seq:16,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_inspect_details.html'},
                        {panel_code:'spc_details',panel_name:$filter('translate')('QT05.sheet.spc_details.title'),seq:17,visible:true,active:false,url:'module/moduleQT/program/QT05/panel/QT05_spc_details.html'},
                        {panel_code:'wait_details',panel_name:$filter('translate')('QT05.sheet.wait_details.title'),seq:18,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_wait_details.html'},
                        {panel_code:'backwait_details',panel_name:$filter('translate')('QT05.sheet.backwait_details.title'),seq:19,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_backwait_details.html'},
                        {panel_code:'dualunit_details',panel_name:$filter('translate')('QT05.sheet.dualunit_details.title'),seq:20,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_dualunit_details.html'},
                        {panel_code:'qc_reason_details',panel_name:$filter('translate')('QT05.sheet.qc_reason_details.title'),seq:21,visible:false,active:false,url:'module/moduleQT/program/QT05/panel/QT05_qc_reason_details.html'}
                    ],
                    loadPanelList:[],
                    headerTitle: $filter('translate')('QT05.prefix') + '｜' + $filter('translate')('QT05.title'),
                    model_no: '1',
                    dashboardPanel: [],
                    tabElem: $('.info-panel'),
                    model_name: '',
                    init:function()
                    {
                        $css.add('JSplugins/angular-ui-tree/angular-ui-tree.min.css');
                        main_event.bom_query_setting = getQuerySetting();
                        var content = {};
                        content.base_data = {};
                        content.base_data.item_code = main_event.bom_query_setting.item_code;
                        content.base_data.item_name = main_event.bom_query_setting.item_name;
                        content.base_data.item_spec = main_event.bom_query_setting.item_spec;
                        content.base_data.lot_code = main_event.bom_query_setting.lot_no;
                        main_event.create_bom_tree_menu(content);
                        main_event.panel_load_data(main_event.bom_query_setting.lot_no);
                    },
                    search_page_change_icon:function(){
                        changeMainPage('module/moduleQT/program/QT05/QT05_Query_Page.html');
                    },
                    model_load_open_icon:function(){
                        $mdDialog.serviceDialog('LoadTemplateService', main_event.model_no, function(data){
                            main_event.dashboardPanel = data;
                            main_event.panel_list.forEach(function(panel) {
                                panel.visible = false;
                                panel.active = false;
                            })
                            main_event.dashboardPanel.mode_sheets.forEach(function(sheet, index) {
                                var panel = $filter('filter')(main_event.panel_list, {panel_code: sheet.mode_sheet})[0];
                                panel.visible = true;
                                panel.seq = sheet.seq;
                            })
                            var firstPanel = $filter('filter')(main_event.panel_list, {seq: 1})[0];
                            firstPanel.active = true;
                            var loadTemplateLength = main_event.dashboardPanel.mode_sheets.length;
                            main_event.panel_list.forEach(function(panel, index) {
                                if (!panel.visible){
                                    panel.seq = index + loadTemplateLength + 1;
                                }
                            })
                        });
                    },
                    model_save_open_icon:function(){
                        var option = {
                            model_no: main_event.model_no,
                            panel_list: main_event.panel_list
                        }
                        $mdDialog.serviceDialog('SaveTemplateService', option, function(model_name) {
                            main_event.model_name = model_name;
                        });
                    },
                    panel_item_menu_open:function(){
                        if (main_event.panel_list.length === 0) return
                        var option = {
                            model_name: main_event.model_name,
                            panel_list: angular.copy(main_event.panel_list)
                        }
                        $mdDialog.serviceDialog('QT05ModelSettingService', option, function(sortedPanelList)
                        {   
                            main_event.panel_list = sortedPanelList;
                        });
                    },
                    panel_tab_active:function(panel_item){
                        for(var i in main_event.panel_list)
                            main_event.panel_list[i].active = false;
                        panel_item.active = true;    
                        $('.info-list').scrollTop(0);
                    },
                    create_bom_tree_menu:function(content)
                    {
                        qualityTraceInfoGet(content,function(data) 
                        {
                            if(data.bom_level_details && data.bom_level_details.length > 0)
                            {
                                main_event.bom_detail = data.bom_level_details;
                                if(main_event.bom_detail.length > 0)
                                {
                                    var max_lev = 0;
                                    main_event.bom_detail = 
                                    [{
                                        "parent_item_code": "P120902",
                                        "parent_item_feature_code": "1",
                                        "parent_lot_code": "348A-10912240002-000",
                                        "sub_item_code": "",
                                        "sub_item_feature_code": "",
                                        "sub_lot_code": "",
                                        "lev": "0",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        
                                        "parent_item_code": "P120902",
                                        "parent_item_feature_code": "1",
                                        "parent_lot_code": "348A-10912240002-000",
                                        "sub_item_code": "P120903",
                                        "sub_item_feature_code": "2",
                                        "sub_lot_code": "348A-10912240002-001",
                                        "lev": "1",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        "parent_item_code": "P120903",
                                        "parent_item_feature_code": "2",
                                        "parent_lot_code": "348A-10912240002-001",
                                        "sub_item_code": "P120904",
                                        "sub_item_feature_code": "3",
                                        "sub_lot_code": "348A-10912240002-002",
                                        "lev": "2",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        "parent_item_code": "P120903",
                                        "parent_item_feature_code": "2",
                                        "parent_lot_code": "348A-10912240002-001",
                                        "sub_item_code": "P120905",
                                        "sub_item_feature_code": "4",
                                        "sub_lot_code": "348A-10912240002-003",
                                        "lev": "2",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        "parent_item_code": "P120905",
                                        "parent_item_feature_code": "4",
                                        "parent_lot_code": "348A-10912240002-003",
                                        "sub_item_code": "P120906",
                                        "sub_item_feature_code": "5",
                                        "sub_lot_code": "348A-10912240002-004",
                                        "lev": "3",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        
                                        "parent_item_code": "P120909",
                                        "parent_item_feature_code": "1",
                                        "parent_lot_code": "348A-10912240032-000",
                                        "sub_item_code": "P120912",
                                        "sub_item_feature_code": "3",
                                        "sub_lot_code": "348A-10912240032-002",
                                        "lev": "1",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        
                                        "parent_item_code": "P120909",
                                        "parent_item_feature_code": "1",
                                        "parent_lot_code": "348A-10912240032-000",
                                        "sub_item_code": "P120911",
                                        "sub_item_feature_code": "2",
                                        "sub_lot_code": "348A-10912240032-001",
                                        "lev": "1",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        "parent_item_code": "P120912",
                                        "parent_item_feature_code": "3",
                                        "parent_lot_code": "348A-10912240032-002",
                                        "sub_item_code": "P120913",
                                        "sub_item_feature_code": "4",
                                        "sub_lot_code": "348A-10912240032-005",
                                        "lev": "2",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        "parent_item_code": "P120911",
                                        "parent_item_feature_code": "2",
                                        "parent_lot_code": "348A-10912240032-001",
                                        "sub_item_code": "P120915",
                                        "sub_item_feature_code": "8",
                                        "sub_lot_code": "348A-10912240032-007",
                                        "lev": "2",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        "parent_item_code": "P120911",
                                        "parent_item_feature_code": "2",
                                        "parent_lot_code": "348A-10912240032-001",
                                        "sub_item_code": "P120914",
                                        "sub_item_feature_code": "7",
                                        "sub_lot_code": "348A-10912240032-006",
                                        "lev": "2",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    },
                                    {
                                        "parent_item_code": "P120915",
                                        "parent_item_feature_code": "8",
                                        "parent_lot_code": "348A-10912240032-007",
                                        "sub_item_code": "P120919",
                                        "sub_item_feature_code": "15",
                                        "sub_lot_code": "348A-10912240032-009",
                                        "lev": "3",
                                        "user_id": "1",
                                        "qid": "1",
                                        "ent": "1",
                                        "site": "1",
                                        "run_time": "2021-09-02 17:51:25"
                                    }];
                                    for(var i in main_event.bom_detail)
                                    {
                                        var item = main_event.bom_detail[i];
                                        main_event.bom_detail[i].nodes=[];
                                        main_event.bom_detail[i].show_line = false;
                                        main_event.bom_detail[i].tree_toggle = main_event.tree_toggle;
                                        main_event.bom_detail[i].lot_info = main_event.lot_info;
                                        main_event.bom_detail[i].current_lot_code= item.sub_lot_code;
                                        if(item.lev == 0)
                                            main_event.bom_detail[i].current_lot_code= item.parent_lot_code;
                                        if((+item.lev) > max_lev)
                                            max_lev = (+item.lev);
                                    }
                                    for(var i = max_lev ; i >= 0 ; i--)
                                    {
                                        var next = i-1;
                                        if(next >= 0)
                                        {
                                            for(var j = 0 ; j < main_event.bom_detail.length;j++)
                                            {
                                                var item = main_event.bom_detail[j];
                                                var lev = item.lev;
                                                var sub_item_code = item.sub_item_code;
                                                var sub_item_feature_code = item.sub_item_feature_code;
                                                var sub_lot_code = item.sub_lot_code;
                                                if(next == lev)
                                                {
                                                    if(next == 0)
                                                    {
                                                        sub_item_code = item.parent_item_code;
                                                        sub_item_feature_code = item.parent_item_feature_code;
                                                        sub_lot_code =item.parent_lot_code;
                                                    }
                                                    var find_current_query = {}
                                                    find_current_query.lev = i;
                                                    find_current_query.parent_item_code = sub_item_code;
                                                    find_current_query.parent_item_feature_code = sub_item_feature_code;
                                                    find_current_query.parent_lot_code = sub_lot_code;
                                                    var current_bom = $filter('filter')(main_event.bom_detail,find_current_query)
                                                    if(current_bom.length > 0)
                                                        current_bom[0].show_line = true;
                                                    item.nodes = current_bom;
                                                    main_event.bom_detail[j] = item;
                                                }
                                            }
                                        }
                                    }
                                    main_event.bom_detail = $filter('filter')(main_event.bom_detail,{lev:0});
                                }
                            }
                            $timeout(function(){
                                var uiTreeScope = angular.element(document.getElementById("tree-root")).scope();
                                var childNodes = uiTreeScope.childNodes();
                                if(childNodes && childNodes.length > 0)
                                    main_event.bom_select_id = (+ childNodes[0].$handleScope.$id );
                            });
                        });
                    },
                    tree_toggle:function(scope,bom_item)
                    {
                        scope.toggle();
                        main_event.bom_select_id = (+scope.$id);
                    },
                    tree_collapse: function()
                    {
                        var uiTreeScope = angular.element(document.getElementById("tree-root")).scope();
                        var childNodes = uiTreeScope.childNodes();
                        var first_node_open_count = $filter('filter')(childNodes,{collapsed:false}).length;
                        childNodes[0].toggle();
                        if(first_node_open_count <= 0)
                        {
                            angular.element(document.getElementsByClassName("tree-panel")).addClass('change');
                            angular.element(document.getElementsByClassName("info-panel")).addClass('change');
                        }
                        else
                        {
                            angular.element(document.getElementsByClassName("tree-panel")).removeClass('change');
                            angular.element(document.getElementsByClassName("info-panel")).removeClass('change');
                        }
                    },
                    panel_load_data:function(lot_code)
                    {
                        var has_visible = $filter('filter')(main_event.panel_list,{visible:true});
                        var content = {};
                        content.subset_query_conditon = [];
                        for(var i in has_visible)
                        {
                            var c_obj = {};
                            c_obj.mode_type = has_visible[i].panel_code;
                            c_obj.lot_code = lot_code;
                            content.subset_query_conditon.push(c_obj);
                        }
                        qualityTraceInfoGet(content,function(data)
                        {
                            if(data) {
                                main_event.all_panel_data = data;

                                var scrollContainer = document.querySelector(".tab-container");

                                scrollContainer.addEventListener("wheel", (evt) => {
                                    evt.preventDefault();
                                    scrollContainer.scrollLeft += evt.deltaY;
                                });
                            }
                        });
                    },
                    lot_info: function(node, event)
                    {
                        // 点击后打开靠右抽屉显示批号详情
                        var data = {
                            level: node.$nodeScope.$modelValue.lev,
                            item_code: node.$nodeScope.$modelValue.parent_item_code,
                            item_name: main_event.bom_query_setting.item_name,
                            item_spec: main_event.bom_query_setting.item_spec,
                            lotno: main_event.bom_query_setting.lot_no,
                            lot_code: node.$nodeScope.$modelValue.parent_lot_code,
                        }
                        
                        $mdDialog.serviceDialog('QT05PlotInfoService', data);
                    },
                    download: function()
                    {
                        var data = {
                            list: angular.copy(main_event.panel_list),
                            lot_code: main_event.bom_query_setting.lot_no
                        }

                        $mdDialog.serviceDialog('QT05DownloadService', data);
                    },
                    sheet_grid_open: function(sheetName) {
                        setTargetSheet(sheetName);
                        var sheetData = main_event.all_panel_data[sheetName];
                        setSheetData(sheetData);
                        changeMainPage('module/moduleQT/program/QT05/QT05_Sheet_Main.html');
                    },
                    getPanelTitle: function(titleName) {
                        return titleName + $filter('translate')('QT05.details')
                    }
                };
                return main_event;
            }
            
            var SheetGridEvent = function()
            {
                var  SheetGridEvent = 
                {
                    targetSheet: '',
                    sheetData: [],
                    title: '',
                    url: '',
                    PageConfig: {
                        page: 0,
                        page_count: 25
                    },
                    init:function() 
                    {   
                        SheetGridEvent.targetSheet = getTargetSheet();
                        SheetGridEvent.title = $filter('translate')('QT05.sheet.' + SheetGridEvent.targetSheet + '.title') + $filter('translate')('QT05.details');
                        SheetGridEvent.sheetData = getSheetData();
                        SheetGridEvent.url = 'module/moduleQT/program/QT05/sheet/QT05_' + SheetGridEvent.targetSheet + '.html';
                    },
                    isConfirmBtnDisabled: function() {
                        return $filter('filter')(SheetGridEvent.query_setting_detail,{is_select:true},true).length <= 0;
                    },
                    back() {
                        changeMainPage('module/moduleQT/program/QT05/QT05_Main.html');
                    },
                    backToMain() {
                        changeMainPage('module/moduleQT/program/QT05/QT05_Main.html');
                    },
                };

                return SheetGridEvent;
            };

        }
];});
